package com.wuyou.designPatterns.observer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 用于生成数值的抽象类
 *
 * @Date 2024/10/15 17:19
 * @Author 无忧
 */
public abstract class NumberGenerator {

    private List<Observer> observers = new ArrayList();

    /**
     * 注册Observer
     * @param observer
     */
    public void addObserver(Observer observer){
        observers.add(observer);
    }

    /**
     * 删除Observer
     * @param observer
     */
    public void deleteObserver(Observer observer){
        observers.remove(observer);
    }

    public void notifyObservers(){
        Iterator<Observer> iterator = observers.iterator();
        while (iterator.hasNext()){
            Observer observer = iterator.next();
            observer.update(this);
        }
    }

    /**
     * 获取数值
     * @return
     */
    public abstract int getNumber();

    /**
     * 生成数值
     */
    public abstract void execute();
}
